//
//  XMLReader.m
//  HanBlog
//
//  Created by zou leeging on 10-10-10.
//  Copyright 2010 apple. All rights reserved.
//

#import "XMLReader.h"


@implementation XMLReader

@synthesize delegate;

-(void)getBlogs {

	blogArray = [[NSMutableArray alloc]init];
	
	BlogConnection *connection = [[BlogConnection alloc]init];
	connection.delegate = self;
	[connection performGetBlog:@"http://blog.sina.com.cn/rss/1191258123.xml"];
	[connection release];
}

-(void)blogConnection:(BlogConnection *)connection didReceivedData:(NSData *)data
{
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
	parser.delegate = self;
	[parser parse];
	
	[parser release];
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	
	
	if ([elementName isEqualToString:@"item"]) {
		
		m_blog = [[Blog alloc]init];
		
	}
	
	else if ([elementName isEqualToString:@"title"]) {
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"pubDate"])
		currentString = [NSMutableString string];
	
	else if ([elementName isEqualToString:@"description"])
		currentString = [NSMutableString string];
	

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     
	
	if ([elementName isEqualToString:@"item"]) {
		[blogArray addObject:m_blog];
		[m_blog release];
		m_blog = nil;
	}
	
	else if ([elementName isEqualToString:@"title"]) {
	
		m_blog.title = currentString;
		currentString = nil;
	}
	else if([elementName isEqualToString:@"pubDate"]) {
	
		m_blog.creatAt = currentString;
		currentString = nil;
	}
	else if([elementName isEqualToString:@"description"]) {
	
		m_blog.content = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"rss"]) {
	
		[delegate XMLReader:self didGetBlogs:blogArray];
		//NSLog(@"%@",blogArray);
	}
	
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	if (currentString) {
		[currentString appendString:string];
	}
}

@end
